/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainPane.java
 *
 * Created on 2010-7-24, 19:00:47
 */
package com.weed.client.login;

import com.weed.client.component.ext.Div;
import com.weed.client.component.ext.RLoading;
import com.weed.client.components.outlook.ROutlook;
import com.weed.client.components.outlook.ROutlookBar;
import com.weed.client.components.outlook.ROutlookItem;
import com.weed.client.components.outlook.ROutlookList;
import com.weed.client.components.statusbar.RStatusBar;
import com.weed.client.components.util.SkinPanel;
import com.weed.client.components.util.SkinUtil;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.event.ListSelectionEvent;
import org.util.grid.DataGrid;

/**
 *
 * @author Jason
 */
public class MainPane extends SkinPanel implements ActionListener {

    SkinPanel topcomponent = new SkinPanel("topcomponent");
    private ROutlook outlook = new ROutlook() {
       public void valueChanged(ListSelectionEvent e) {
            clientAction(e);
        }
    };
    RStatusBar bottomcomponent = new RStatusBar();
   // private SkinPanel centerComponent = new SkinPanel("workcomponent");
    private JPanel centerComponent = new JPanel();
    JToolBar topbar = new JToolBar();
    DataGrid datagrid = null;
    String srcColumn = null;
    protected  HeadButton clickButton = null;
    private  HeadButton onebutton = null;
    /** Creates new form MainPane */
    public MainPane() {
        super("maincomponent");
        //initComponents();
        //this.setForeground(Color.white);
        init();
        
    }

    protected void init() {
        this.setLayout(new BorderLayout());
        this.add(topcomponent, BorderLayout.NORTH);
        this.add(getOutlook(), BorderLayout.WEST);
        this.add(bottomcomponent, BorderLayout.SOUTH);
        this.add(getCenterComponent(), BorderLayout.CENTER);
        centerComponent.setLayout(new BorderLayout());
        topcomponent.setLayout(new BorderLayout());
        this.topcomponent.add(topbar, BorderLayout.CENTER);
        this.topbar.setOpaque(false);
        this.topbar.setFloatable(false);
        this.setBorder(null); 
        
    }

    public void setDataGrid(DataGrid datagrid) {
        this.datagrid = datagrid;
        datagridToComponent();
    }

    /**
     *  格式要求：code,name,icon,parentcode,parentlevel,action
     * 设置需要显示的列。
     * @param str
     */
    public void setSrcColumn(String srcColumn) {
        this.srcColumn = srcColumn;
        datagridToComponent();
    }

    /**
     *
     * 值到组件。
     * @param source
     * @param datagrid
     * @param srcColumn
     */
    public void datagridToComponent() {
        if (srcColumn == null || datagrid == null) {
            System.out.println("srcColumn:" + srcColumn);
            System.out.println("dataGrid:" + datagrid);
            return;
        }
        String[] values = srcColumn.split(",");
        System.out.println("values:" + values.length);
        //格式要求：code,name,icon,parentcode,parentlevel,action
        
        for (int index = 0; index < datagrid.getRowCount(); index++) {
            HashMap map = datagrid.getRowMap(index);
            String code = (String) map.get(values[0]);
            String name = (String) map.get(values[1]);
            String icon = (String) map.get(values[2]);
            String parentcode = (String) map.get(values[3]);
            String action = (String) map.get(values[4]);
            HashMap temp = null;
            if (parentcode == null || "".equals(parentcode.trim())) {
                HeadButton button = createHeadButton(code, name, icon, parentcode, action);
                this.topbar.add(button);
                if(index ==0 )onebutton =button;
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 439, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 362, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
   
    public HeadButton createHeadButton(String code, String name, String icon, String parentcode, String action) {
         HeadButton button = new HeadButton() {
           public void mouseClicked(MouseEvent e) {
               clickButton = this;
               super.mouseClicked(e);
                Div div = new Div() {
                        protected Object doInBackground() throws Exception {
                            clientAction(clickButton);
                            Thread.sleep(500);
                            return "";
                        }
                 };
                    //div.showGlassPane(this,new RLoading());
                  div.showLayerPane(outlook.getCenterPane(),new RLoading());
                  div.start();
            }
        };
        button.setBorder(BorderFactory.createEmptyBorder(3, 3, 4, 3));
        button.setText(name);
        button.setCode(code);
        button.setParentcode(parentcode);
        button.setActionString(action);
        button.setIcon(SkinUtil.getImageIcon(icon));
        button.setOpaque(false);
        return button;
    }

    public void clientAction(HeadButton button) {
        String[] values = srcColumn.split(",");
        String _code = button.getCode();
        HashMap map_temp = new HashMap();
        HashMap map_temp1 = new HashMap();
        for (int index = 0; index < datagrid.getRowCount(); index++) {
            HashMap map = datagrid.getRowMap(index);
            String code = (String) map.get(values[0]);
            String name = (String) map.get(values[1]);
            String icon = (String) map.get(values[2]);
            String parentcode = (String) map.get(values[3]);
            String action = (String) map.get(values[4]);
            if (_code.equalsIgnoreCase(parentcode)) {
                map_temp.put(code, map);
                map_temp1.put(code, new ArrayList<ROutlookItem>());
            }
        }
        HashMap map_tmep2 = new HashMap();
        for (int index = 0; index < datagrid.getRowCount(); index++) {
            HashMap map = datagrid.getRowMap(index);
            String code = (String) map.get(values[0]);
            String name = (String) map.get(values[1]);
            String icon = (String) map.get(values[2]);
            String parentcode = (String) map.get(values[3]);
            String action = (String) map.get(values[4]);
            Set set = map_temp1.keySet();
            if (set.contains(parentcode)) {
                ArrayList<ROutlookItem> temp = (ArrayList<ROutlookItem>) map_temp1.get(parentcode);
                ROutlookItem item = new ROutlookItem();
                ImageIcon tempicon = SkinUtil.getImageIcon(icon);
                if (tempicon == null || tempicon.getIconHeight() < 0 || tempicon.getIconWidth() < 0) {
                    tempicon = SkinUtil.getImageIcon("images/style_fxy.gif", 492, 132, 509, 149);
                }
                item.setIcon(tempicon);
                item.setText(name);
                item.setToolTip(name);
                item.setShrinked(true);
                item.setActionCommand(action);
                temp.add(item);
            }
        }
        Iterator iterator = map_temp.keySet().iterator();
        Icon icon = SkinUtil.getImageIcon("images/style_fxy.gif", 492, 132, 509, 149);
        Icon selectedIcon = SkinUtil.getImageIcon("images/style_fxy.gif", 492, 132, 509, 149);
        this.outlook.removeAllBar();
        int i = 0;
        ROutlookBar bar = null;
        while (iterator.hasNext()) {
            String code = (String) iterator.next();
            HashMap map = (HashMap) map_temp.get(code);
            String name = (String) map.get(values[1]);
            String action = (String) map.get(values[4]);
            ArrayList<ROutlookItem> outlookitems = (ArrayList<ROutlookItem>) map_temp1.get(code);
            ROutlookItem[] items = new ROutlookItem[outlookitems.size()];
            for (int index = 0; index < items.length; index++) {
                items[index] = outlookitems.get(index);
            }
            if (i == 0) {
                bar = this.outlook.addBar(name, icon, selectedIcon, items);
            } else {
                this.outlook.addBar(name, icon, selectedIcon, items);
            }
            i++;
        }
        if (bar != null) {
            bar.setSelected(true);
        }
        this.outlook.revalidate();
//      /  this.doLayout();
        //outlook.doLayout();
    }

    /**
     * @return the outlook
     */
    public ROutlook getOutlook() {
        return outlook;
    }

    /**
     * @param outlook the outlook to set
     */
    public void setOutlook(ROutlook outlook) {
        this.outlook = outlook;
    }

    public void actionPerformed(ActionEvent e) {
    }

    /**
     * @return the centerComponent
     */
    public JPanel getCenterComponent() {
     //  return centerComponent;
        return centerComponent;
    }
    
    /**
     * @param centerComponent the centerComponent to set
     */
    public void setCenterComponent(SkinPanel centerComponent) {
        this.centerComponent = centerComponent;
    }

    public void showPane(final String pane) {
        Div div = new Div() {
            @Override
         protected Object doInBackground() throws Exception {
                if (pane != null) {
                    Class clazz;
                    try {
                        clazz = Class.forName(pane);
                        Object obj = clazz.newInstance();
                        if (obj != null) {
                            getCenterComponent().removeAll();
                            getCenterComponent().add((JComponent) obj, BorderLayout.CENTER);
                            //   this.rPanel1.setVisible(true);
                            Thread.currentThread().sleep(1000);
                        }
                    } catch (Exception ex) {
                        Logger.getLogger(MainPane.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                return null;
            }
        };
        div.showGlassPane(this,new RLoading());
        //div.showLayerPane(centerComponent,new RLoading());
        div.start();
    }

    public void clientAction(ListSelectionEvent e) {
        ROutlookList list = (ROutlookList) e.getSource();
        ROutlookItem item = (ROutlookItem) list.getSelectedValue();
        String action = item.getActionCommand();
        if (action != null) {
            showPane(action);
        }
    }

    /**
     * @return the onebutton
     */
    public HeadButton getOnebutton() {
        return onebutton;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
